package com.fsc.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.fsc._enum.ReturnEnum;
import com.fsc.entity.ResultWrapper;
import com.fsc.entity.domain.PictureVideo;
import com.fsc.service.LoginInfoService;
import com.fsc.service.PictureVideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.fsc._enum.ReturnEnum.*;

@RestController
@RequestMapping("/attendance")
public class PictureVideoController {
    @Autowired
    private PictureVideoService pictureVideoService;

    @Autowired
    private LoginInfoService loginInfoService;

    @GetMapping("/{studentId}")
    public ResultWrapper<List<PictureVideo>> getAttendanceByStudentId(@PathVariable int studentId) {
        return ResultWrapper.success(SUCCESS, pictureVideoService.getAttendanceByStudentId(studentId));
    }
    @GetMapping
    public ResultWrapper<List<PictureVideo>> getAllAttendance() {
        Integer loginId = Integer.parseInt((String) StpUtil.getLoginId());
        int role = loginInfoService.getRole(loginId);

        List<PictureVideo> attendanceList;
        if (role == 1) {  // 角色是老师
            attendanceList = pictureVideoService.getAttendanceByTeacherId(loginId);
        } else if (role == 2) {  // 角色是家长
            attendanceList = pictureVideoService.getAttendanceByParentId(loginId);
        } else if (role == 3) {  // 角色是学生
            attendanceList = pictureVideoService.getAttendanceByStudentId(loginId);
        }else if(role == 0){
            attendanceList = pictureVideoService.getAllAttendance();
        } else {
            return ResultWrapper.fail(Not_Found);
        }

        return ResultWrapper.success(SUCCESS, attendanceList);
    }
    @PostMapping("/upload")
    public ResultWrapper<String> uploadImage(@RequestParam("file") MultipartFile file,
                                             @RequestParam("studentId") int studentId,
                                             @RequestParam("time") @DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX") Date time) {
        try {
            // 调用服务层的方法上传图片并更新数据库记录
            String url = pictureVideoService.uploadImage(file, studentId, time);
            return ResultWrapper.success(SUCCESS, url);
        } catch (IOException e) {
            e.printStackTrace(); // 确保记录异常
            return ResultWrapper.fail(UPLOAD_FAIL);
        }
    }
    @PostMapping("/add")
    public ResultWrapper<String> addAttendance(
            @RequestParam("file") MultipartFile file,
            @RequestParam("studentId") int studentId,
            @RequestParam("status") String status,
            @RequestParam("time") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date time) throws IOException {
        pictureVideoService.addAttendance(file, studentId, status, time);
        return ResultWrapper.success("Attendance added successfully");
    }

    @PutMapping("/update")
    public ResultWrapper<String> updateAttendance(
            @RequestParam("id") int id,
            @RequestParam("status") String status,
            @RequestParam("time") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date time) {
        pictureVideoService.updateAttendance(id, status, time);
        return ResultWrapper.success("Attendance updated successfully");
    }

    @DeleteMapping("/delete/{id}")
    public ResultWrapper<String> deleteAttendance(@PathVariable int id) {
        pictureVideoService.deleteAttendance(id);
        return ResultWrapper.success("Attendance deleted successfully");
    }
}
